package com.grace.law.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grace.law.bean.Dict;
import com.grace.law.bean.Msg;
import com.grace.law.bean.Result;
import com.grace.law.bean.TreeNode;
import com.grace.law.service.DictService;
import com.grace.law.utils.StrUtil;

@Controller
@RequestMapping(value = "/dict")
public class DictController {

    @Resource
    private DictService dictService;

    /**
     * 用户列表
     */
    @RequestMapping(method = RequestMethod.GET, value = "/tree")
    private String list() {

        return "base/dict/dict_tree";
    }

    @RequestMapping(value = "/all", method = RequestMethod.POST)
    @ResponseBody
    public List<Dict> getAll() {

        String hql = "from Dict order by levelCode asc";
        return dictService.find(hql.toString());
    }

    /**
     * getTreeData 构造bootstrap-treeview格式数据
     *
     * @return
     */
    @RequestMapping(value = "/treeData", method = RequestMethod.POST)
    @ResponseBody
    public List<TreeNode> getTreeData() {

        return dictService.getTreeData();
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Dict get(@PathVariable("id") String id) {

        Dict dict = dictService.get(id);
        if (!StrUtil.isEmpty(dict.getParentId())) {
            dict.setParentName(dictService.get(dict.getParentId()).getName());
        } else {
            dict.setParentName("系统字典");
        }
        return dict;
    }

    @RequestMapping(value="/getDictsByCode",method = RequestMethod.POST)
    @ResponseBody
    public List<Dict> getDictsByCode(String code){
        return dictService.getDictsByCode(code);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody Dict dict) {
    	if(StringUtils.isEmpty(dict.getId())){
    		String id = StringUtils.replace(UUID.randomUUID().randomUUID().toString(), "-", "");
        	dict.setId(id);
        	dict.setCreateDateTime(new Date());
        	dictService.insert(dict);
    	}else{
    		dict.setUpdateDateTime(new Date());
    		dictService.update(dict);
    	}
    	
        
        
        return new Result(true);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@PathVariable("id") String id) {

        try {
            dictService.delete(id);
            return new Result(true);
        } catch (Exception e) {
            return new Result(false, "该字典已经被其他数据引用，不可删除");
        }
    }

    
    @RequestMapping("/getdicts")
	@ResponseBody
	public Msg getDictsWithJson() {
		
		List<Dict> dicts = dictService.getAll();
		// 使用pageInfo包装查询后的结果，只需要将pageInfo交给页面就行了。
		// 封装了详细的分页信息,包括有我们查询出来的数据，传入连续显示的页数
		return Msg.success().add("dicts", dicts);
	}
}
